package com.htdata.iiot.capture;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.eclipse.paho.client.mqttv3.MqttClient;

public class BridgeProfile {
	public static BridgeProfile bridgeProfile = new BridgeProfile();

	private String jarAbsPath;
	private String propConfFile;

	private boolean isBenchmark;
	private String[] mosquitoBrokerArray;
	private String mosquitoClientGUID;
	private String mosquitoUserName;
	private String mosquitoPassWord;
	private String[] mosquitoTopicArray;
	private String[] mosquitoKeyArray;
	private int[] mosquitoQosArray;
	
	private String[] emqttBrokerArray;
	private String emqttClientGUID;
	private String emqttUserName;
	private String emqttPassWord;
	private String emqttTopic;
	private int    emqttQos;
	private int    emqttDnSize;
	private Logger logger;
	
	private BridgeProfile() {
		URI uri = null;
		File file = null;
		logger = Logger.getLogger(BridgeProfile.class);

		try {
			uri = BridgeProfile.class.getClassLoader().getResource("").toURI();
			file = new File(uri);
			if (file.isFile() || file.getPath().endsWith(".jar") || (".zip").equals(file.getPath())) {
				jarAbsPath = file.getParent();
			} else {
				jarAbsPath = file.getPath();
			}

			System.setProperty("jarAbsPath", jarAbsPath);

		

			propConfFile = Paths.get(jarAbsPath, "conf", "mosquito2emqtt.properties").toString();
			loadContent();
		} catch (Exception e) {
			if (logger == null) {
				System.out.println(e.getMessage());
			} else {
				logger.error(e.getMessage());
			}
			System.exit(0);
		}
	}

	private void loadContent() throws Exception {
		File file = new File(propConfFile);
		FileInputStream fStream = new FileInputStream(file);
		Properties properties = new Properties();
		properties.load(fStream);

		String benchmarkStr = properties.getProperty("benchmark");
		if (benchmarkStr != null) {
			benchmarkStr = benchmarkStr.trim();
		}
		if ("1".equals(benchmarkStr) || "true".equals(benchmarkStr)) {
			isBenchmark = true;
		} else {
			isBenchmark = false;
		}
		//------------------mosquito----------------------------------------
		String mosbrokerList = properties.getProperty("mosquito.brokerlist");
		if (mosbrokerList == null) {
			throw new ExceptionInInitializerError("mosquito.brokerlist in '" + propConfFile + "' must be provided!");
		}

		mosbrokerList = mosbrokerList.trim();
		if (mosbrokerList.isEmpty()) {
			throw new ExceptionInInitializerError("mosquito.brokerlist in '" + propConfFile + "' must not be empty!");
		}

		mosquitoBrokerArray = mosbrokerList.split(",");
		
		mosquitoClientGUID = properties.getProperty("mosquito.clientGUID");
		if (mosquitoClientGUID != null) {
			mosquitoClientGUID = mosquitoClientGUID.trim();
		}

		if (mosquitoClientGUID == null || mosquitoClientGUID.isEmpty()) {
			mosquitoClientGUID = MqttClient.generateClientId();
		}

		mosquitoUserName = properties.getProperty("mosquito.username","test");
		mosquitoUserName = mosquitoUserName.trim();
		if (mosquitoUserName.isEmpty()) {
			mosquitoUserName = "test";
		}
		
		mosquitoPassWord = properties.getProperty("mosquito.password","test");
		mosquitoPassWord = mosquitoPassWord.trim();
		if (mosquitoPassWord.isEmpty()) {
			mosquitoPassWord = "test";
		}
		
		String mosquitoTopicList = properties.getProperty("mosquito.topiclist", "/#");
		mosquitoTopicList = mosquitoTopicList.trim();
		if (mosquitoTopicList.isEmpty()) {
			mosquitoTopicList = "/#";
		}

		mosquitoTopicArray = mosquitoTopicList.split(",");
		
		
		String mosquitoKeyList = properties.getProperty("mosquito.keyList", "/#");
		mosquitoKeyList = mosquitoKeyList.trim();
		if (mosquitoKeyList.isEmpty()) {
			throw new ExceptionInInitializerError("mosquito.keyList in '" + propConfFile + "' must not be empty!");
		}

		mosquitoKeyArray = mosquitoKeyList.split(",");

		String mosquitoQosList = properties.getProperty("mosquito.qoslist", "0");
		mosquitoQosList = mosquitoQosList.trim();
		if (mosquitoQosList.isEmpty()) {
			mosquitoQosList = "0";
		}
			
		String[] mosquitoQosStrArray = mosquitoQosList.split(",");
		mosquitoQosArray = new int[mosquitoQosStrArray.length];
		int i = 0;
		for (; i < mosquitoQosStrArray.length; ++i) {
			mosquitoQosArray[i] = Integer.valueOf(mosquitoQosStrArray[i]);
		}

		//------------------emqtt----------------------------------------
		String emqbrokerList = properties.getProperty("emqtt.brokerlist");
		if (emqbrokerList == null) {
			throw new ExceptionInInitializerError("emqtt.brokerlist in '" + propConfFile + "' must be provided!");
		}

		emqbrokerList = emqbrokerList.trim();
		if (emqbrokerList.isEmpty()) {
			throw new ExceptionInInitializerError("emqtt.brokerlist in '" + propConfFile + "' must not be empty!");
		}

		emqttBrokerArray = emqbrokerList.split(",");
		
		emqttClientGUID = properties.getProperty("emqtt.clientGUID");
		if (emqttClientGUID != null) {
			emqttClientGUID = emqttClientGUID.trim();
		}

		if (emqttClientGUID == null || emqttClientGUID.isEmpty()) {
			emqttClientGUID = MqttClient.generateClientId();
		}

		emqttUserName = properties.getProperty("emqtt.username","test");
		emqttUserName = emqttUserName.trim();
		if (emqttUserName.isEmpty()) {
			emqttUserName = "test";
		}
		
		emqttPassWord = properties.getProperty("emqtt.password","test");
		emqttPassWord = emqttPassWord.trim();
		if (emqttPassWord.isEmpty()) {
			emqttPassWord = "test";
		}
		
		
		emqttTopic = properties.getProperty("emqtt.topic", "/test");

		String emqttQosStr = properties.getProperty("emqtt.qos", "0");
		if(emqttQosStr == null){
			emqttQos = 0;
		}
		emqttQos = Integer.valueOf(emqttQosStr);
		
		

		String emqttDnSizeStr = properties.getProperty("emqtt.dnSize", "1");
		if(emqttDnSizeStr == null || emqttDnSizeStr.equals("")){
			emqttDnSize = 1;
		}
		emqttDnSize = Integer.valueOf(emqttDnSizeStr);
		
		fStream.close();
	}
	
	

	public String[] getMosquitoKeyArray() {
		return mosquitoKeyArray;
	}

	public void setMosquitoKeyArray(String[] mosquitoKeyArray) {
		this.mosquitoKeyArray = mosquitoKeyArray;
	}

	public int getEmqttDnSize() {
		return emqttDnSize;
	}

	public void setEmqttDnSize(int emqttDnSize) {
		this.emqttDnSize = emqttDnSize;
	}

	public String getJarAbsPath() {
		return jarAbsPath;
	}

	public String getPropConfFile() {
		return propConfFile;
	}

	public boolean getBenchmark() {
		return isBenchmark;
	}

	public String[] getMosquitoBroker() {
		return mosquitoBrokerArray;
	}

	public String getMosquitoClientId() {
		return mosquitoClientGUID;
	}

	public String getMosquitoUserName() {
		return mosquitoUserName;
	}
	
	public String getMosquitoPassWord() {
		return mosquitoPassWord;
	}
	
	public String[] getMosquitoTopic() {
		return mosquitoTopicArray;
	}

	public int[] getMosquitoQos() {
		return mosquitoQosArray;
	}
	
	public String[] getEmqttBroker() {
		return emqttBrokerArray;
	}
	
	public String getEmqttClientId() {
		return emqttClientGUID;
	}

	public String getEmqttUserName() {
		return emqttUserName;
	}
	
	public String getEmqttPassWord() {
		return emqttPassWord;
	}
	
	public String getEmqttTopic() {
		return emqttTopic;
	}
	
	public int getEmqttQos() {
		return emqttQos;
	}
	
	public static  BridgeProfile getBridgeProfile() {
		return bridgeProfile;
	}

	public  void setBridgeProfile(BridgeProfile bridgeProfile) {
		bridgeProfile = bridgeProfile;
	}

}
